package org.example.util;

public class StringToByteArray {
    public static byte[] hexStringToByteArray(String hexString) {
        // 移除空格和换行符
        hexString = hexString.replaceAll("\\s+", "");

        int len = hexString.length();
        byte[] byteArray = new byte[len / 2];

        for (int i = 0; i < len; i += 2) {
            byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }

        return byteArray;
    }
    public static String byteArrayToHexString(byte[] bytes,int len){
        // 使用StringBuilder来构建字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append(String.format("%02X", bytes[i]));
        }
        return sb.toString();

    }
}
